package com.xiaomai.eduservice.controller;


import com.xiaomai.commonutil.R;
import com.xiaomai.eduservice.entity.EduChapter;
import com.xiaomai.eduservice.entity.chapter.ChapterVo;
import com.xiaomai.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-30
 */
@RestController
@RequestMapping("/eduservice/chapter")

public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService; //注入service对象
//定义根据id获取章节及小节
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){

       List<ChapterVo> list =  eduChapterService.getChapterVideoByCourseId(courseId);//调用方法返回章节与小节的list对象
        return R.ok().data("allChapterVideo",list);//将list对象返回给前端
    }

    //添加章节
    @PostMapping("addChapter")
    private R addChapter(@RequestBody EduChapter eduChapter){
         eduChapterService.save(eduChapter);
        return R.ok();
    }

    //根据id查到章节
    @GetMapping("getChapterInfo/{chapterId}")
    private R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }
    //根据id修改章节
    @PostMapping("updateChapter")
    private R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }
    //根据id删除章节
    @DeleteMapping("deleteChapter/{chapterId}")
    private R deleteChapter(@PathVariable String chapterId){
        boolean flag = eduChapterService.deleteById(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

